package com.ty.interceptors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.smart.core.model.UserSession;
import cn.smart.core.util.UserContext;
import com.ty.config.MonitorTableConfig;
import com.ty.entity.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.update.UpdateSet;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.*;
@Component
@Intercepts(@Signature(
        type = StatementHandler.class,
        method = "update",
        args = {Statement.class}))
//1. 实现Interceptor
//2. 指定拦截类型 以及方法和参数 @Intercepts(@Signature(
@Slf4j
public class StatementInterceptor implements Interceptor {

    //   private static final List<String> MINOTOR_TABLE = CollUtil.newArrayList("212_product");
    //   private static final Map<String,List<String>> MINOTOR_MAP = new HashMap<>();
    @Autowired
    private MonitorTableConfig monitorTableConfig;
    //    static {
//        MINOTOR_MAP.put("212_product",CollUtil.newArrayList("name","brief"));
//    }
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        StatementHandler statementHandler =   (StatementHandler)(invocation.getTarget());
        String sql = statementHandler.getBoundSql().getSql();
        net.sf.jsqlparser.statement.Statement parse = CCJSqlParserUtil.parse(sql);
        if(parse instanceof Insert){
            insertHandler((Insert) parse);
        } else if (parse instanceof Update){
            updateHandler((Update) parse);
        } else {
            deleteHandler((Delete) parse);
        }
        return invocation.proceed(); //继续执行
    }
    private void  updateHandler(Update update){
        Table table = update.getTable();
        Map<String, List<String>> map = monitorTableConfig.getTable();

        Set<String> keys = map.keySet();
        if(keys.contains(table.getName())){

            List<UpdateSet> updateSets = update.getUpdateSets();
            List<String> columnList = map.get(table.getName());
            for (UpdateSet updateSet : updateSets) {

                String columnName = updateSet.getColumns().get(0).getColumnName();
                if(columnList.contains(columnName) && ObjectUtil.isNotEmpty(UserContext.get())){
                    sendDingding(StrUtil.format("{}更改了表：{}，字段：{}", UserContext.get().getNickName(), table.getName(), columnName));
                }
            }
        }
    }
    private void  insertHandler(Insert insert){
        Table table = insert.getTable();
//        Map<String, List<String>> tables = monitorTableConfig.getTables();
//        Set<String> keys = tables.keySet();
//        if(keys.contains(table.getName())){
//            //发送钉钉
//            String nickName = UserContext.get().getNickName();
//
//            log.debug("{} 插入了 {}", nickName, table.getName());
//        }
    }

    private void  deleteHandler(Delete delete){
        Table table = delete.getTable();
//        Map<String, List<String>> tables = monitorTableConfig.getTables();
//        Set<String> keys = tables.keySet();
//        if(keys.contains(table.getName())){
//            //发送钉钉
//        }
    }
    private  void  sendDingding(String msg){
        log.debug("告警："+msg);
        //发送钉钉
    }
}
